package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.logic.cross.CrossGroupWeekRankListModel;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.zhaoshang.ZsRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.C_ZHAO_SHANG_PROJECT_TOP_OF_RANK, desc = "榜首")
public class ZsWeekRankTopSyncCmd  extends CrossCommand {

    private int weekId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.CrossZsWeekRankTopReqMsg reqMsg = ZhaoShangProto.CrossZsWeekRankTopReqMsg.parseFrom(packet.getBytes());
        weekId = reqMsg.getWeekId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int groupId = ZsRankMgr.getGroupId(serverId);
        Map<Integer, CrossGroupWeekRankListModel> rankListModelMap = ZsRankMgr.getCrossGroupWeekRankListModel(groupId, weekId, eBigRankType.Normal.getValue());
        ZhaoShangProto.ZsWeekRankTopRespMsgFromCross.Builder respMsg = ZhaoShangProto.ZsWeekRankTopRespMsgFromCross.newBuilder();
        if(rankListModelMap != null){
            for(int key : rankListModelMap.keySet()){
                CrossGroupWeekRankListModel model = rankListModelMap.get(key);
                RankProto.TopRankTemp.Builder topRankTemp = RankProto.TopRankTemp.newBuilder();
                if(model.getRankListMsg().getRankListCount() > 0) {
                    RankProto.RankTempMsg msg = model.getRankListMsg().getRankList(0);
                    topRankTemp.setRankTemp(msg);
                }
                topRankTemp.setType(key);
                respMsg.addRankTopList(topRankTemp);
            }
            respMsg.setWeekId(weekId);
            MessageHelper.sendPacket(serverId, 0,  YanQuMessageUtils.buildMessage(Protocol.S_ZS_PROJECT_TOP_OF_RANK_FROM_CROSS, respMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
